package org.wedding.center.service.impl;

import cn.hutool.core.util.StrUtil;
import org.wedding.dto.PageModel;
import org.wedding.center.dto.query.ArticleQuery;
import org.wedding.center.entity.ArticleInfo;
import org.wedding.center.mapper.ArticleInfoMapper;
import org.wedding.center.service.ArticleInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wedding.enums.system.ResultCodeEnum;
import org.wedding.utils.CommonResult;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import org.wedding.utils.PageUtil;

import java.util.List;

/**
 * 首页文章表(ArticleInfo)表服务实现类
 *
 * @author songkaiyue
 * @since 2025-05-14 16:25:46
 */
@Service
public class ArticleInfoServiceImpl extends ServiceImpl<ArticleInfoMapper, ArticleInfo> implements ArticleInfoService {
    /**
     * 分页查询所有数据
     *
     * @return
     */
    @Override
    public CommonResult<PageModel> findAll(ArticleQuery query) {
        try {
            if (StrUtil.isNotEmpty(query.getIsRandom())&&"0".equals(query.getIsRandom())){
                List<ArticleInfo> randomList = baseMapper.selectRandom(6); // 假设selectRandom方法已经实现
                List<ArticleInfo> setList = query.setList(PageUtil.getListPage(query.getCurrent(), query.getPageSize(), randomList), randomList.size());
                query.setList(setList, randomList.size());
                return CommonResult.success(query);
            }
            List<ArticleInfo> list = baseMapper.selectAll(query);
            List<ArticleInfo> setList = query.setList(PageUtil.getListPage(query.getCurrent(), query.getPageSize(), list), list.size());
            query.setList(setList, list.size());
            return CommonResult.success(query);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 根据id查看详情
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult<ArticleInfo> view(String id) {
        try {
            ArticleInfo articleInfo = baseMapper.findById(id);
            return CommonResult.success(articleInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param articleInfo
     * @return
     */
    @Override
    public CommonResult add(ArticleInfo articleInfo) {
        try {
            articleInfo.setId(IdUtil.simpleUUID());
            articleInfo.setCreateTime(DateUtil.now());
            articleInfo.setDelFlag("0");
            articleInfo.setSortNum(0);
            Integer maxSortNum = baseMapper.findMaxSortNum();
            Integer sortNum = getSortNum(articleInfo.getSortNum(), maxSortNum);
            articleInfo.setSortNum(sortNum);
            if (StrUtil.isEmpty(articleInfo.getIsCommend())){
                articleInfo.setIsCommend("1");
            }
            baseMapper.insert(articleInfo);
            return CommonResult.success(articleInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

    /**
     * 递归获取排序号
     *
     * @param sortNum
     * @param maxSortNum
     * @return
     */
    private Integer getSortNum(Integer sortNum, Integer maxSortNum) {
        if (sortNum == null) {
            if (maxSortNum != null) {
                return maxSortNum + 1;
            }
            return 0;
        } else {
            ArticleInfo info = baseMapper.findBySortNum(sortNum);
            if (info != null) {
                int next = sortNum + 1;
                return getSortNum(next, maxSortNum);
            }
            return sortNum;
        }
    }

    /**
     * 修改数据
     *
     * @param articleInfo
     * @return
     */
    @Override
    public CommonResult updateEntity(ArticleInfo articleInfo) {
        try {
            ArticleInfo info = baseMapper.findById(articleInfo.getId());
            if (!articleInfo.getSortNum().equals(info.getSortNum())) {
                Integer maxSortNum = baseMapper.findMaxSortNum();
                Integer sortNum = getSortNum(articleInfo.getSortNum(), maxSortNum);
                articleInfo.setSortNum(sortNum);
            }
            articleInfo.setUpdateTime(DateUtil.now());
            baseMapper.updateById(articleInfo);
            return CommonResult.success(articleInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

      /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResult delete(List<String> ids) {
        try {
            if (ObjectUtil.isEmpty(ids)) {
                return CommonResult.failed(ResultCodeEnum.PARAM_ERROR);
            }
            for (String id : ids) {
                baseMapper.upDeleteById(id);
            }
            return CommonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ResultCodeEnum.API_ERROR);
        }
    }

}
